#include "polynomial.h"
#include <cstdlib>
//
// Constructors/Destructors
//

/**
 * Empty Constructor
 */
polynomial::polynomial()
{
    initAttributes();
}

/**
 * Constructor
 */
polynomial::polynomial(int order, ...)
{
    initAttributes();
    //double FindAverage(int nCount, ...)
    //long lSum = 0;
    //va_list list;
    //va_start(list, nCount);
    //for (int nArg=0; nArg < nCount; nArg++)
    //    lSum += va_arg(list, int);
    //va_end(list);
    va_list list;
    va_start(list, (order+1));
    m_order = order;
    m_constants = new float[order];
    m_constants1DRV = new float[order-1];
    m_constants2DRV = new float[order-2];
    m_constants3DRV = new float[order-3];
    for (int i = 0; i <= order; i++)
    {
        double temp = va_arg(list, double);
        m_constants[i] = temp;
        if (i > 0)
        {
            m_constants1DRV[i-1] = ((double)truncFactorial(i,1))*temp;
            if (i > 1)
            {
                m_constants2DRV[i-2] = ((double)truncFactorial(i,2))*temp;
                if (i > 2)
                {
                    m_constants3DRV[i-3] = ((double)truncFactorial(i,3))*temp;
            }
        }
    }
    }
    va_end(list);
}

/**
 * Empty Destructor
 */
polynomial::~polynomial ( )
{
    if ( m_constants != NULL)
        delete [] m_constants;
    if ( m_constants1DRV != NULL)
        delete [] m_constants1DRV;
    if ( m_constants2DRV != NULL)
        delete [] m_constants2DRV;
    if ( m_constants3DRV != NULL)
        delete [] m_constants3DRV;
}

//
// Methods
//

/**
 * Solve two polynomials ...
 */
double polynomial::solveForConst(polynomial* p1, polynomial* p2)
{
    //getOrder...
    return 0.0;
}

//
// Accessor methods
//

//
// Other methods
//

/**
 * Initialization function of class variables.
 */
void polynomial::initAttributes ( )
{
    m_order = 0;
    m_constants = NULL;
    m_constants1DRV = NULL;
    m_constants2DRV = NULL;
    m_constants3DRV = NULL;
}

/**
 * Multiplies base by desending factorial for n levels
 * @return product
 * @param base is the starting number
 * @param n is the number of times the base will be multiplied
 */
int polynomial::truncFactorial(int base, int n)
{
    int product = base;
    for (int i = 0; i < n; i++)
    {
        product *= base - i;
    }
    return product;
}
